
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Convert a file path to a file url: `unicorn.jpg` → `file:///Users/sindresorhus/unicorn.jpg`
Supply Chain Security
Vulnerability
Quality
Maintenance
License
The file-url npm package is a utility that converts file paths to file URLs. This can be particularly useful when working with file systems in Node.js and needing to generate URLs that can be used in web contexts.
Convert file path to file URL
This feature allows you to convert a given file path to a file URL. The resulting URL can be used in web contexts where a file URL is required.
const fileUrl = require('file-url');
const url = fileUrl('/path/to/file');
console.log(url); // Outputs: 'file:///path/to/file'
Convert file path to file URL with custom options
This feature allows you to convert a file path to a file URL with additional options. For example, the 'resolve' option can be used to resolve the file path to an absolute path before converting it to a URL.
const fileUrl = require('file-url');
const url = fileUrl('/path/to/file', { resolve: true });
console.log(url); // Outputs: 'file:///absolute/path/to/file'
The url package is a core Node.js module that provides utilities for URL resolution and parsing. While it is more general-purpose and not specifically focused on file URLs, it can be used to achieve similar results with more manual effort.
The file-uri-to-path package does the reverse of file-url by converting file URLs back to file paths. It can be used in conjunction with file-url for bidirectional conversions between file paths and URLs.
Convert a file path to a file url:
unicorn.jpg
→file:///Users/sindresorhus/unicorn.jpg
$ npm install file-url
import fileUrl from 'file-url';
fileUrl('unicorn.jpg');
//=> 'file:///Users/sindresorhus/dev/file-url/unicorn.jpg'
fileUrl('/Users/pony/pics/unicorn.jpg');
//=> 'file:///Users/pony/pics/unicorn.jpg'
fileUrl('unicorn.jpg', {resolve: false});
//=> 'file:///unicorn.jpg'
Returns the filePath
converted to a file URL.
Type: string
File path to convert.
Type: object
Type: boolean
Default: true
Passing false
will make it not call path.resolve()
on the path.
FAQs
Convert a file path to a file url: `unicorn.jpg` → `file:///Users/sindresorhus/unicorn.jpg`
The npm package file-url receives a total of 671,783 weekly downloads. As such, file-url popularity was classified as popular.
We found that file-url demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.